package net.lovesrc.allin.biz.jietu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.lovesrc.allin.Message;
import net.lovesrc.allin.biz.gt.GtService;
import net.lovesrc.allin.biz.stl.StlService;
import net.lovesrc.allin.bo.Device;
import net.lovesrc.allin.controller.BaseController;
import net.lovesrc.allin.utils.MyRandomUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.UriUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

@Controller
@RequestMapping("/jietu")
public class JieTuController extends BaseController {

    @Autowired
    private StlService stlService;

    @Autowired
    private JieTuService jieTuService;

    @Autowired
    private GtService gtService;

    @GetMapping("/stl/alipay")
    public String stl(Model model, Integer jyId) {
        int rand = RandomUtils.nextInt(1, 101);
        JieTuOuterContext outerContext = stlService.createJieTuOuterContext(jyId);
        if (rand >= 100 - tryGetProperties(int.class, 20, "Stl_JieTu_Android_Percent")) {
            // android
            JieTuAndroidAlipayContext context = jieTuService.createAndroidAlipayContext(outerContext);
            model.addAllAttributes(JSON.parseObject(JSON.toJSONString(context)));
            return "jietu/alipay-android";
        } else {
            JieTuIosAlipayContext context = jieTuService.createIosAlipayContext(outerContext);
            model.addAllAttributes(JSON.parseObject(JSON.toJSONString(context)));
            return "jietu/alipay-ios";
        }
    }

    @GetMapping("/gt/alipay")
    public String gt(Model model, Integer jyId) {
        JieTuOuterContext outerContext = gtService.createJieTuOuterContext(jyId);
        if (outerContext.getDevice() == Device.Android) {
            // android
            JieTuAndroidAlipayContext context = jieTuService.createAndroidAlipayContext(outerContext);
            model.addAllAttributes(JSON.parseObject(JSON.toJSONString(context)));
            return "jietu/alipay-android";
        } else {
            JieTuIosAlipayContext context = jieTuService.createIosAlipayContext(outerContext);
            model.addAllAttributes(JSON.parseObject(JSON.toJSONString(context)));
            return "jietu/alipay-ios";
        }
    }


    @GetMapping("/test")
    @ResponseBody
    public Message test() {
        jieTuService.newJieTu("http://localhost:8080/jietu/stl/alipay?jyId=77135", "stl-jy-77135");
        return Message.success();
    }

    @GetMapping("/lbc/alipay")
    public String lbcAlipay(Model model, String jyId) {

        JSONObject row = queryForRow(jdbcTemplate, "select * from t_lbc_jy where jyId = ?", jyId);
        String buyerMobile = row.getString("buyerMobile");
        JSONObject buyer = queryForRow(jdbcTemplate, "select * from t_lbc_account where mobile = ?", buyerMobile);
        String sellerMobile = row.getString("sellerMobile");
        JSONObject seller = queryForRow(jdbcTemplate, "select * from t_lbc_account where mobile = ?", sellerMobile);
        BigDecimal amount = row.getBigDecimal("amount");
        String buyerPayMethod = MyRandomUtils.randomPayMethod();
        String sellerRealName = seller.getString("realName");

        JieTuOuterContext outerContext = JieTuOuterContext.builder()
                .buyerMobile(buyerMobile)
                .buyerPayMethod(buyerPayMethod)
                .amount(amount)
                .sellerRealName(sellerRealName)
                .build();
        int rand = RandomUtils.nextInt(1, 101);
        if (rand >= 100 - tryGetProperties(int.class, 20, "JieTu_Android_Percent")) {
            // android
            JieTuAndroidAlipayContext context = jieTuService.createAndroidAlipayContext(outerContext);
            model.addAllAttributes(JSON.parseObject(JSON.toJSONString(context)));
            return "jietu/alipay-android";
        } else {
            JieTuIosAlipayContext context = jieTuService.createIosAlipayContext(outerContext);
            model.addAllAttributes(JSON.parseObject(JSON.toJSONString(context)));
            return "jietu/alipay-ios";
        }
    }

    @GetMapping("/pay/alipay")
    public String payAlipay(String text, Model model) throws UnsupportedEncodingException {
        model.addAttribute("text", UriUtils.encode(text, "utf-8"));
        model.addAttribute("userLogoUrl", jieTuService.randomUserLogo());
        return "jietu/pay/alipay";
    }

    @GetMapping("/pay/wechat")
    public String payWechat(String text, Model model) throws UnsupportedEncodingException {
        model.addAttribute("text", UriUtils.encode(text, "utf-8"));
        return "jietu/pay/wechat";
    }

}
